#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
#include <stdlib.h>
#include <stdio.h>

Column* CreateCol(int size, char name)
{
	if (size <= 0) return NULL;
	Column* col = (Column*)malloc(sizeof(Column));
	if (!col) return NULL;
	col->base = (Ring*)malloc(sizeof(Ring) * size);
	if (!col->base)
	{
		free(col);
		return NULL;
	}
	col->top = -1;
	col->length = size;
	col->name = name;
	return col;
}

void InitCol(Column* col)
{
	for (int i = 0; i < col->length; i++)
	{
		col->base[++col->top] = col->length - i;
	}
}

int IsColEmpty(const Column* col)
{
	return col->top == -1;
}

int IsColFull(const Column* col)
{
	return col->length == col->top + 1;
}

int MoveRing(Column* c1, Column* c2)
{
	if (IsColEmpty(c1) || c1 == c2)
		return 0;
	if (!IsColEmpty(c2) && c1->base[c1->top] > c2->base[c2->top])
		return 0;
	c2->base[++c2->top] = c1->base[c1->top--];
	return 1;
}

void PrintColInfo(const Column* c)
{
	printf("%c : ", c->name);
	for (int i = 0; i <= c->top; i++)
	{
		printf("%d ", c->base[i]);
	}
	printf("\n");
}

void DestroyCol(Column** c)
{
	Column* col = *c;
	free(col->base);
	free(col);
	*c = NULL;
}

void ShowBestAnswer(int n, char a, char b, char c)
{
	if (n > 1)
	{
		ShowBestAnswer(n - 1, a, c, b);
		printf("%c => %c\n", a, c);
		ShowBestAnswer(n - 1, b, a, c);
	}
	else
	{
		printf("%c => %c\n", a, c);
	}
}